//
//  NSDate+Debug.m
//  EverVCiOSClient
//
//  Created by Oz on 14-5-29.
//  Copyright (c) 2014年 EverVC. All rights reserved.
//

#import "NSDate+Debug.h"

@implementation NSDate (ISO_8601_String)

static NSDateFormatter *dateFormatterFor_yyyy_MM_dd__HH__mm__ss = nil;
static NSDateFormatter *dateFormatterFor_yyyy_MM_dd__HH__mm__ss__SSS = nil;

+ (NSDateFormatter *)get_dateFormatterFor_yyyy_MM_dd__HH__mm__ss {
    if (!dateFormatterFor_yyyy_MM_dd__HH__mm__ss) {
        dateFormatterFor_yyyy_MM_dd__HH__mm__ss =  [[NSDateFormatter alloc] init];
        [dateFormatterFor_yyyy_MM_dd__HH__mm__ss setDateFormat:@"yyyy-MM-dd' 'HH:mm:ss"];
    }
    return dateFormatterFor_yyyy_MM_dd__HH__mm__ss;
}


+ (NSString *)yy_MM_WithTimestamp:(NSString *)timestamp {
    NSDate *time = [NSDate dateWithTimeIntervalSince1970:[timestamp longLongValue]];
    
    static NSDateFormatter *yy_MM_Formatter = nil;
    if (!yy_MM_Formatter) {
        yy_MM_Formatter = [[NSDateFormatter alloc] init];
        [yy_MM_Formatter setDateFormat:@"yyyy年MM月"];
    }
    NSString *result = [yy_MM_Formatter stringFromDate:time];
    return result;

}
+ (NSString *)yy_MM_ddWithTimestamp:(NSString *)timestamp {
    NSDate *time = [NSDate dateWithTimeIntervalSince1970:[timestamp longLongValue]/1000];
    
    return [time dateString_yyyy_MM_dd];
}
///  2018-04-26 13：26：33
+ (NSString *)yy_MM_dd_HH_mm__ssWithTimestamp:(NSString *)timestamp {
    NSDate *time = [NSDate dateWithTimeIntervalSince1970:[timestamp longLongValue]];
    return [time dateTimeStringFor_yyyy_MM_dd__HH__mm__ss];
}

+ (NSString *)t_1_yy_MM_dd_TimeString {
    NSDate *time = [NSDate dateWithTimeIntervalSinceNow:86400];
    
    return [time dateString_yyyy_MM_dd];
}

+ (NSString *)MM_ddWithTimestamp:(NSString *)timestamp {
    NSDate *time = [NSDate dateWithTimeIntervalSince1970:[timestamp longLongValue]/1000];
    
    return [time dateStringFor_MM_dd];
}

+ (NSString *)yyyy_MM_ddOrMM_ddWithTimestamp:(NSString *)timestamp {
    if (timestamp.length < 5) {
        return @"";
    }
    NSDate *time = [NSDate dateWithTimeIntervalSince1970:[timestamp longLongValue]];
    if ([time isToday] == YES) {
        return [time dateTimeStringForHHmm];
        
    } else if ([time isThisYear] == YES) {
        return [time dateStringFor_MM_dd];
    } else {
        return [time dateString];
    }
}

///  05-08 13:30
+ (NSString *)MM_dd_HH_mmWithTimestamp:(id)timestamp {
    NSDate *time = [NSDate dateWithTimeIntervalSince1970:[timestamp longLongValue]];
    
    static NSDateFormatter *MM_dd_HH_mm_Formatter = nil;
    if (MM_dd_HH_mm_Formatter == nil) {
        MM_dd_HH_mm_Formatter = [[NSDateFormatter alloc] init];
        [MM_dd_HH_mm_Formatter setDateFormat:@"MM-dd' 'HH:mm"];
    }
    NSString *result = [MM_dd_HH_mm_Formatter stringFromDate:time];
    return result;
}

+ (NSDate *)dateWithDateFor_yyyy_MM_dd__HH__mm__ss_SSS_String:(NSString *)dateStr {
    if (dateFormatterFor_yyyy_MM_dd__HH__mm__ss__SSS == nil) {
        dateFormatterFor_yyyy_MM_dd__HH__mm__ss__SSS = [[NSDateFormatter alloc] init];
        [dateFormatterFor_yyyy_MM_dd__HH__mm__ss__SSS setDateFormat:@"yyyy-MM-dd' 'HH:mm:ss.SSS"];
    }
   return [dateFormatterFor_yyyy_MM_dd__HH__mm__ss__SSS dateFromString:dateStr];
}

+ (NSString *)todayDateStringFor_yyyy_MM_dd__HH__mm__ss_SSS {
    NSDate *date = date = [NSDate date];
    NSString *dateStr = [date dateString_yyyy_MM_dd];
    
    NSString *todayDate = [NSString stringWithFormat:@"%@ 00:00:00.000", dateStr];
    return todayDate;
}
/// 获取明天0点前一秒 2017-05-12 00:00:00.000
+ (NSString *)tomorrowDateStringFor_yyyy_MM_dd__HH__mm__ss_SSS {
    NSDate *date = [NSDate date];
    NSString *dateStr = [date dateString_yyyy_MM_dd];
    
    NSString *tomorrowDate = [NSString stringWithFormat:@"%@ 23:59:59.000", dateStr];
    return tomorrowDate;
}

///  获取月份： 8月
+ (NSString *)monthStringWithTimestamp:(NSString *)timestamp {
    
    NSDate *time = [NSDate dateWithTimeIntervalSince1970:[timestamp longLongValue]];
    
    NSCalendar *calendar = [ NSCalendar currentCalendar];
    
    int unit = NSCalendarUnitMonth;
    
    // 2. 获得 self 的年月日
    NSDateComponents *selfCmps = [calendar components :unit fromDate : time ];
    return [NSString stringWithFormat:@"%ld月", selfCmps.month];
}
+ (NSString *)stringFor_yyyy_MM_ddWithDateString:(NSString *)dateString {
    NSDate *date = [[NSDate get_dateFormatterFor_yyyy_MM_dd__HH__mm__ss] dateFromString:dateString];
    
    return [date dateString_yyyy_MM_dd];
}
///  2000-01-01 00:00:00
//+ (NSString *)stringFor_yyyy_MM_dd__HH__mm__ssWithDateString:(NSString *)dateString {
////    NSDate *date = [NSDate get_dateFormatterFor_yyyy_MM_dd__HH__mm__ss]
//}

+ (NSString *)DayStringWithTimestamp:(NSString *)timestamp {
    NSDate *time = [NSDate dateWithTimeIntervalSince1970:[timestamp longLongValue]];
    NSCalendar *calendar = [ NSCalendar currentCalendar];
    
    int unit = NSCalendarUnitDay;
    
    // 2. 获得 self 的年月日
    NSDateComponents *selfCmps = [calendar components :unit fromDate : time ];
    return [NSString stringWithFormat:@"%ld", selfCmps.day];

}
+ (NSString *)DayStringWithDateString:(NSString *)dateStr {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSDate *time = [dateFormatter dateFromString:dateStr];
    
    NSCalendar *calendar = [ NSCalendar currentCalendar];
    
    int unit = NSCalendarUnitDay;
    
    // 2. 获得 self 的年月日
    NSDateComponents *selfCmps = [calendar components :unit fromDate : time ];
    return [NSString stringWithFormat:@"%ld", selfCmps.day];
}


///  获取当前时间戳（北京时间）
+ (NSString *)currentTimestamp {
    
    NSDate *date = [NSDate date];
    NSString *timestamp = [NSString stringWithFormat:@"%ld", (long)[date timeIntervalSince1970]];
    return timestamp;
//    return [timestamp stringByAppendingString:@"000"];
}
+ (BOOL)currentIsDisplayTimeWithTimestamp:(NSString *)timestamp {
    long currentTimestamp = [[NSDate date] timeIntervalSince1970];
    long targetTimestamp = [timestamp longLongValue];
    
    if ((currentTimestamp - targetTimestamp) > (60*5)) {
        return YES;
    }
    return NO;
}
/// 是否超过30天
+ (BOOL)isExceed30DayWithTagetTimestamp:(NSString *)timestamp {
    long currentTimestamp = [[NSDate date] timeIntervalSince1970];
    long targetTimestamp = [timestamp longLongValue];
    if ((currentTimestamp - targetTimestamp) > (30*24*60*60)) {
        return YES;
    }
    return NO;
}
+ (BOOL)isExceed2MinuteWithTargetTimestamp:(NSString *)timestamp {
    long currentTimestamp = [[NSDate date] timeIntervalSince1970];
    long targetTimestamp = [timestamp longLongValue];
    if ((currentTimestamp - targetTimestamp) > (2*60)) {
        return YES;
    }
    return NO;
}
- (NSString *)dateString_yyyy_MM_dd__HH__mm__ss
{
    static NSDateFormatter *dateFormatter= nil;
    if (!dateFormatter) {
        dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"yyyy-MM-dd' 'HH:mm:ss"];
    }
    NSString *result = [dateFormatter stringFromDate:self];
    return result;
}

- (NSString *)dateTimeString2
{
    static NSDateFormatter *dateFormatter= nil;
    if (!dateFormatter) {
        dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"MM-dd' 'HH:mm"];
    }
    NSString *result = [dateFormatter stringFromDate:self];
    return result;
}

- (NSString *)dateTimeStringForHHmm {
    static NSDateFormatter *dateFormatterForHHmm= nil;
    if (!dateFormatterForHHmm) {
        dateFormatterForHHmm = [[NSDateFormatter alloc] init];
        [dateFormatterForHHmm setDateFormat:@"HH:mm"];
    }
    NSString *result = [dateFormatterForHHmm stringFromDate:self];
    return result;
}
- (NSString *)dateTimeStringFor_yyyy_MM_dd__HH__mm {
    static NSDateFormatter *dateFormatterFor_yyyy_MM_dd__HH__mm = nil;
    if (!dateFormatterFor_yyyy_MM_dd__HH__mm) {
        dateFormatterFor_yyyy_MM_dd__HH__mm = [[NSDateFormatter alloc] init];
        [dateFormatterFor_yyyy_MM_dd__HH__mm setDateFormat:@"yyyy-MM-dd' 'HH:mm:ss"];
    }
    NSString *result = [dateFormatterFor_yyyy_MM_dd__HH__mm stringFromDate:self];
    return result;
}
///  2017-05-11 12:06:50
- (NSString *)dateTimeStringFor_yyyy_MM_dd__HH__mm__ss {
    NSDateFormatter *yyyy_MM_dd__HH_mm_ssFormatter = [NSDate get_dateFormatterFor_yyyy_MM_dd__HH__mm__ss];
    NSString *result = [yyyy_MM_dd__HH_mm_ssFormatter stringFromDate:self];
    return result;
}
///  2017-05-11 12:06:50.123
- (NSString *)dateTimeStringFor_yyyy_MM_dd__HH__mm__ss_SSS {
    if (!dateFormatterFor_yyyy_MM_dd__HH__mm__ss__SSS) {
        dateFormatterFor_yyyy_MM_dd__HH__mm__ss__SSS = [[NSDateFormatter alloc] init];
        [dateFormatterFor_yyyy_MM_dd__HH__mm__ss__SSS setDateFormat:@"yyyy-MM-dd' 'HH:mm:ss.SSS"];
    }
    NSString *result = [dateFormatterFor_yyyy_MM_dd__HH__mm__ss__SSS stringFromDate:self];
    return result;

}


- (NSString *)dateString
{
    static NSDateFormatter *dateFormatter= nil;
    if (!dateFormatter) {
        dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"yyyy年MM月dd日"];
    }
    NSString *result = [dateFormatter stringFromDate:self];
    return result;
}



- (NSString *)dateString_yyyy_MM_dd;
{
    static NSDateFormatter *dateFormatter= nil;
    if (!dateFormatter) {
        dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    }
    NSString *result = [dateFormatter stringFromDate:self];
    return result;
}

-(NSString *) formated_ISO_8601_TimeString{
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
    
    [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'Z'"];
    
    NSString *result = [dateFormatter stringFromDate:self];
    return result;
}

//+ (NSString *)prettyDateTimeString:(NSTimeInterval)timeInterval
//{
//    static NSDateFormatter *prettyDateFormatter1= nil;
//    static NSDateFormatter *prettyDateFormatter2= nil;
//    if (!prettyDateFormatter1) {
//        prettyDateFormatter1 = [[NSDateFormatter alloc] init];
//        [prettyDateFormatter1 setDateFormat:@"yyyy-MM-dd' 'HH:mm:ss"];
//    }
//    if (!prettyDateFormatter2) {
//        prettyDateFormatter2 = [[NSDateFormatter alloc] init];
//        [prettyDateFormatter2 setDateFormat:@"yyyy-MM-dd"];
//    }
//    
//    NSString *result = [prettyDateFormatter1 stringFromDate:];
//    return result;
//}

- (NSString *)dateStringFor_MM_dd {
    
    static NSDateFormatter *mothAndDayFormatter = nil;
    if (!mothAndDayFormatter) {
        mothAndDayFormatter = [[NSDateFormatter alloc] init];
        [mothAndDayFormatter setDateFormat:@"MM月dd日"];
    }
    NSString *result = [mothAndDayFormatter stringFromDate:self];
    return result;
    
}

- ( BOOL )isToday

{
    
    NSCalendar *calendar = [ NSCalendar currentCalendar ];
    
    int unit = NSCalendarUnitDay | NSCalendarUnitMonth |  NSCalendarUnitYear ;
    
    
    
    // 1. 获得当前时间的年月日
    
    NSDateComponents *nowCmps = [calendar components :unit fromDate :[ NSDate date ]];
    
    
    
    // 2. 获得 self 的年月日
    
    NSDateComponents *selfCmps = [calendar components :unit fromDate : self ];
    
    return
    
    (selfCmps. year == nowCmps. year ) &&      //直接分别用当前对象和现在的时间进行比较，比较的属性就是年月日
    
    (selfCmps. month == nowCmps. month ) &&
    
    (selfCmps. day == nowCmps. day );
    
}

- (BOOL)isThisYear {
    
    NSCalendar *calendar = [ NSCalendar currentCalendar ];
    int unit = NSCalendarUnitDay | NSCalendarUnitMonth |  NSCalendarUnitYear ;
    
    // 1. 获得当前时间的年月日
    NSDateComponents *nowCmps = [calendar components :unit fromDate :[ NSDate date ]];

    // 2. 获得 self 的年月日
    NSDateComponents *selfCmps = [calendar components :unit fromDate : self ];
    
    return selfCmps.year == nowCmps.year;
}


/**
 * @brief 判断当前时间是否在fromHour和toHour之间。如，fromHour=8，toHour=23时，即为判断当前时间是否在8:00-23:30之间
 */
+ (BOOL)isBesideFromHour:(NSInteger)fromHour toHour:(NSInteger)toHour
{
    NSDate *date8 = [self getCustomDateWithHour:fromHour minute:0];
    NSDate *date23 = [self getCustomDateWithHour:toHour minute:30];
    
    NSDate *currentDate = [NSDate date];
    
    if ([currentDate compare:date8]==NSOrderedDescending && [currentDate compare:date23]==NSOrderedAscending)
    {
        NSLog(@"该时间在 %d:00-%d:30 之间！", (int)fromHour, (int)toHour);
        return NO;
    }
    return YES;
}

/**
 * @brief 生成当天的某个点（返回的是伦敦时间，可直接与当前时间[NSDate date]比较）
 * @param hour 如hour为“8”，就是上午8:00（本地时间）
 */
+ (NSDate *)getCustomDateWithHour:(NSInteger)hour minute:(NSInteger)minute
{
    //获取当前时间
    NSDate *currentDate = [NSDate date];
    //    NSCalendar *currentCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSCalendar *currentCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *currentComps = [[NSDateComponents alloc] init];
    
    NSInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    
    currentComps = [currentCalendar components:unitFlags fromDate:currentDate];
    
    //设置当天的某个点
    NSDateComponents *resultComps = [[NSDateComponents alloc] init];
    [resultComps setYear:[currentComps year]];
    [resultComps setMonth:[currentComps month]];
    [resultComps setDay:[currentComps day]];
    [resultComps setHour:hour];
    [resultComps setMinute:minute];
    
    NSCalendar *resultCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    return [resultCalendar dateFromComponents:resultComps];
}

- (NSString *)currentDateString {
    static NSDateFormatter *currentDateStringFormatter = nil;
    if (!currentDateStringFormatter) {
        currentDateStringFormatter = [[NSDateFormatter alloc] init];
        [currentDateStringFormatter setDateFormat:@"yyyy/MM/dd/HH/mm"];
    }
    NSString *result = [currentDateStringFormatter stringFromDate:self];
    return result;
}

+ (NSString *)firstDayOfTheMonth {
    
    NSDate *currentDate = [NSDate date];
    NSCalendar *currentCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *currentComps = [[NSDateComponents alloc] init];
    
    NSInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    currentComps = [currentCalendar components:unitFlags fromDate:currentDate];
    [currentComps setDay:1];
    [currentComps setHour:0];
    [currentComps setMinute:0];
    [currentComps setSecond:0];
    
    NSDate *date = [currentCalendar dateFromComponents:currentComps];
    return [date dateString_yyyy_MM_dd__HH__mm__ss];
}
+ (NSString *)currentTime {
    NSString *currentDate = [[NSDate date] dateString_yyyy_MM_dd__HH__mm__ss];
    return currentDate;
}

@end
